$(function () {
  //  正常写自己的js
  function getherolist() {
    $.ajax({
      type: "get",
      url: "http://localhost:5001/getallhero",
      dataType: "json",
      success: function (res) {
        console.log(res);
        var html = template("hero", res);
        $("#tbd").html(html);
      }
    })
  }
  getherolist();


  //删除英雄
  $("#tbd").on("click", "#del", function () {
    let id = $(this).attr("data-id");
    let del = $(this).attr("data-del")
    console.log(del);
    $.ajax({
      type: "get",
      url: "http://localhost:5001/deletehero/" + del + "/" + id,
      dataType: "json",
      success: function (res) {
        console.log(res);
        if (res.status == 200) {
          getherolist();

        }
      }
    })
  })

  //编辑英雄
  $("#tbd").on("click", "#edit", function () {
    let id = $(this).attr("data-id");
    // alert(1);
    $('.ui.modal').modal('show')
    $(".header").text("编辑英雄");
    $("#btnAdd").text("确定")
      $("#btnAdd").on("click", function () {
        let name = $("#mc").val();
        let gender = $("#gender").val();
        $.ajax({
          type: "post",
          url: "http://localhost:5001/updatehero/" + id,
          dataType: "json",
          data:{
            name:name,
            gender:gender
          },
          success: function (res) {
            console.log(res);
            getherolist();
          }
        })
      })
  })

// 添加英雄
  $('#add').on('click', function () {
    $('.ui.modal').modal('show')
    //添加新英雄

    $("#btnAdd").on("click", function () {
      console.log($(".header").text())
      let hero = $("form").serialize();//键值对
      console.log(hero);
      $.ajax({
        type: "post",
        url: "http://localhost:5001/addhero",
        data: hero,//data不仅可以传对象也可传键值对
        success: function (res) {
          console.log(res);
          if (res.status == 200) {
            getherolist();
          }
        }
      })
    })
  })

  // 彻底删除英雄
  $("#tbd").on("click","#delhero",function(){
    let id=$(this).attr("data-id");
    console.log(id);
    $.ajax({
      type:"get",
      url:"http://localhost:5001/deleteyx/"+ id,
      dataType:"json",
      success:function(res){
        console.log(res);
        if (res.status == 200) {
          getherolist();
        }
      }
    })
  })
  // 双击跳转英雄信息
  $("#tbd").on("dblclick","tr",function(){
    location.href="/heroinfo.html";
    let name=$(this).find(".hname").text();
    let gender=$(this).find(".hgender").text();
    let hctime=$(this).find(".hctime").text();
   let arr=[name,gender,hctime];
   console.log(arr);
   let aa=localStorage.setItem("msg",arr);
  })

  $("")
})